package com.itheima.controller;
import com.itheima.pojo.Address;
import com.itheima.pojo.Result;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
//@RestController就是
//  @ResponseBody+@Controller
//@ResponseBody就是将返回值设置为响应数据
public class ResponseController {
    //响应字符串
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("hello");
        return "hello";
    }

    //响应对象会转json格式
    @RequestMapping("/getAddr")
    public Address getAddr(){
        Address address=new Address();
        address.setProvince("广东");
        address.setCity("深圳");
        return address;
    }
    //响应对象集合会转json格式
    @RequestMapping("/listAddr")
    public List<Address> listAddr(){
        List<Address> addresses=new ArrayList<>();
        Address address1=new Address();
        address1.setProvince("广东");
        address1.setCity("深圳");
        Address address2=new Address();
        address2.setProvince("山西");
        address2.setCity("范钦昱");
        addresses.add(address1);
        addresses.add(address2);
        System.out.println(addresses);
        return addresses;
    }
    //设计模式
    @RequestMapping("/hello2")
    public Result hello2(){
        System.out.println("hello");
        //return new Result(1,"success","hello");
        return Result.success("hello");
    }

    //响应对象会转json格式
    @RequestMapping("/getAddr2")
    public Result getAddr2(){
        Address address=new Address();
        address.setProvince("广东");
        address.setCity("深圳");
        return Result.success(address);
    }
    //响应对象集合会转json格式
    @RequestMapping("/listAddr2")
    public Result listAddr2(){
        List<Address> addresses=new ArrayList<>();
        Address address1=new Address();
        address1.setProvince("广东");
        address1.setCity("深圳");
        Address address2=new Address();
        address2.setProvince("山西");
        address2.setCity("范钦昱");
        addresses.add(address1);
        addresses.add(address2);
        System.out.println(addresses);
        return Result.success(addresses);
    }
}
